// Decompiled on 周六 2月 22 19:31:49 CST 2025 with Zomboid Decompiler v0.1.3 using Vineflower.
package zombie.core.skinnedmodel.advancedanimation;

import java.util.Map;
import java.util.TreeMap;
import zombie.util.StringUtils;
import zombie.util.list.PZArrayUtil;

public class AnimationVariableSource implements IAnimationVariableMap {
    private final Map<String, IAnimationVariableSlot> m_GameVariables = new TreeMap(String.CASE_INSENSITIVE_ORDER);
    private IAnimationVariableSlot[] m_cachedGameVariableSlots = new IAnimationVariableSlot[0];

    @Override
    public IAnimationVariableSlot getVariable(AnimationVariableHandle handle) {
        if (handle == null) {
            return null;
        } else {
            int _int = handle.getVariableIndex();
            if (_int < 0) {
                return null;
            } else if (this.m_cachedGameVariableSlots != null && _int < this.m_cachedGameVariableSlots.length) {
                IAnimationVariableSlot iAnimationVariableSlot0 = this.m_cachedGameVariableSlots[_int];
                if (iAnimationVariableSlot0 == null) {
                    this.m_cachedGameVariableSlots[_int] = (IAnimationVariableSlot)this.m_GameVariables.get(handle.getVariableName());
                    iAnimationVariableSlot0 = this.m_cachedGameVariableSlots[_int];
                }

                return iAnimationVariableSlot0;
            } else {
                IAnimationVariableSlot iAnimationVariableSlot1 = (IAnimationVariableSlot)this.m_GameVariables.get(handle.getVariableName());
                if (iAnimationVariableSlot1 == null) {
                    return null;
                } else {
                    IAnimationVariableSlot[] iAnimationVariableSlot2 = new IAnimationVariableSlot[_int + 1];
                    IAnimationVariableSlot[] iAnimationVariableSlot3 = this.m_cachedGameVariableSlots;
                    if (iAnimationVariableSlot3 != null) {
                        this.m_cachedGameVariableSlots = PZArrayUtil.arrayCopy(
                            iAnimationVariableSlot3, iAnimationVariableSlot2, 0, iAnimationVariableSlot3.length
                        );
                    }

                    iAnimationVariableSlot2[_int] = iAnimationVariableSlot1;
                    this.m_cachedGameVariableSlots = iAnimationVariableSlot2;
                    return iAnimationVariableSlot1;
                }
            }
        }
    }

    @Override
    public IAnimationVariableSlot getVariable(String key) {
        if (StringUtils.isNullOrWhitespace(key)) {
            return null;
        } else {
            String string = key.trim();
            return (IAnimationVariableSlot)this.m_GameVariables.get(string);
        }
    }

    @Override
    public IAnimationVariableSlot getOrCreateVariable(String key) {
        if (StringUtils.isNullOrWhitespace(key)) {
            return null;
        } else {
            String string = key.trim();
            Object object = (IAnimationVariableSlot)this.m_GameVariables.get(string);
            if (object == null) {
                object = new AnimationVariableGenericSlot(string.toLowerCase());
                this.setVariable((IAnimationVariableSlot)object);
            }

            return (IAnimationVariableSlot)object;
        }
    }

    @Override
    public void setVariable(IAnimationVariableSlot var) {
        this.m_GameVariables.put(var.getKey(), var);
    }

    public void setVariable(String key, AnimationVariableSlotCallbackBool.CallbackGetStrongTyped callbackGet) {
        this.setVariable(new AnimationVariableSlotCallbackBool(key, callbackGet));
    }

    public void setVariable(
        String key, AnimationVariableSlotCallbackBool.CallbackGetStrongTyped callbackGet, AnimationVariableSlotCallbackBool.CallbackSetStrongTyped callbackSet
    ) {
        this.setVariable(new AnimationVariableSlotCallbackBool(key, callbackGet, callbackSet));
    }

    public void setVariable(String key, AnimationVariableSlotCallbackString.CallbackGetStrongTyped callbackGet) {
        this.setVariable(new AnimationVariableSlotCallbackString(key, callbackGet));
    }

    public void setVariable(
        String key,
        AnimationVariableSlotCallbackString.CallbackGetStrongTyped callbackGet,
        AnimationVariableSlotCallbackString.CallbackSetStrongTyped callbackSet
    ) {
        this.setVariable(new AnimationVariableSlotCallbackString(key, callbackGet, callbackSet));
    }

    public void setVariable(String key, AnimationVariableSlotCallbackFloat.CallbackGetStrongTyped callbackGet) {
        this.setVariable(new AnimationVariableSlotCallbackFloat(key, callbackGet));
    }

    public void setVariable(
        String key,
        AnimationVariableSlotCallbackFloat.CallbackGetStrongTyped callbackGet,
        AnimationVariableSlotCallbackFloat.CallbackSetStrongTyped callbackSet
    ) {
        this.setVariable(new AnimationVariableSlotCallbackFloat(key, callbackGet, callbackSet));
    }

    public void setVariable(String key, AnimationVariableSlotCallbackInt.CallbackGetStrongTyped callbackGet) {
        this.setVariable(new AnimationVariableSlotCallbackInt(key, callbackGet));
    }

    public void setVariable(
        String key, AnimationVariableSlotCallbackInt.CallbackGetStrongTyped callbackGet, AnimationVariableSlotCallbackInt.CallbackSetStrongTyped callbackSet
    ) {
        this.setVariable(new AnimationVariableSlotCallbackInt(key, callbackGet, callbackSet));
    }

    public void setVariable(String key, boolean defaultVal, AnimationVariableSlotCallbackBool.CallbackGetStrongTyped callbackGet) {
        this.setVariable(new AnimationVariableSlotCallbackBool(key, defaultVal, callbackGet));
    }

    public void setVariable(
        String key,
        boolean defaultVal,
        AnimationVariableSlotCallbackBool.CallbackGetStrongTyped callbackGet,
        AnimationVariableSlotCallbackBool.CallbackSetStrongTyped callbackSet
    ) {
        this.setVariable(new AnimationVariableSlotCallbackBool(key, defaultVal, callbackGet, callbackSet));
    }

    public void setVariable(String key, String defaultVal, AnimationVariableSlotCallbackString.CallbackGetStrongTyped callbackGet) {
        this.setVariable(new AnimationVariableSlotCallbackString(key, defaultVal, callbackGet));
    }

    public void setVariable(
        String key,
        String defaultVal,
        AnimationVariableSlotCallbackString.CallbackGetStrongTyped callbackGet,
        AnimationVariableSlotCallbackString.CallbackSetStrongTyped callbackSet
    ) {
        this.setVariable(new AnimationVariableSlotCallbackString(key, defaultVal, callbackGet, callbackSet));
    }

    public void setVariable(String key, float defaultVal, AnimationVariableSlotCallbackFloat.CallbackGetStrongTyped callbackGet) {
        this.setVariable(new AnimationVariableSlotCallbackFloat(key, defaultVal, callbackGet));
    }

    public void setVariable(
        String key,
        float defaultVal,
        AnimationVariableSlotCallbackFloat.CallbackGetStrongTyped callbackGet,
        AnimationVariableSlotCallbackFloat.CallbackSetStrongTyped callbackSet
    ) {
        this.setVariable(new AnimationVariableSlotCallbackFloat(key, defaultVal, callbackGet, callbackSet));
    }

    public void setVariable(String key, int defaultVal, AnimationVariableSlotCallbackInt.CallbackGetStrongTyped callbackGet) {
        this.setVariable(new AnimationVariableSlotCallbackInt(key, defaultVal, callbackGet));
    }

    public void setVariable(
        String key,
        int defaultVal,
        AnimationVariableSlotCallbackInt.CallbackGetStrongTyped callbackGet,
        AnimationVariableSlotCallbackInt.CallbackSetStrongTyped callbackSet
    ) {
        this.setVariable(new AnimationVariableSlotCallbackInt(key, defaultVal, callbackGet, callbackSet));
    }

    @Override
    public void setVariable(String key, String value) {
        this.getOrCreateVariable(key).setValue(value);
    }

    @Override
    public void setVariable(String key, boolean value) {
        this.getOrCreateVariable(key).setValue(value);
    }

    @Override
    public void setVariable(String key, float value) {
        this.getOrCreateVariable(key).setValue(value);
    }

    @Override
    public void clearVariable(String key) {
        IAnimationVariableSlot iAnimationVariableSlot = this.getVariable(key);
        if (iAnimationVariableSlot != null) {
            iAnimationVariableSlot.clear();
        }
    }

    @Override
    public void clearVariables() {
        for (IAnimationVariableSlot iAnimationVariableSlot : this.getGameVariables()) {
            iAnimationVariableSlot.clear();
        }
    }

    @Override
    public String getVariableString(String key) {
        IAnimationVariableSlot iAnimationVariableSlot = this.getVariable(key);
        return iAnimationVariableSlot != null ? iAnimationVariableSlot.getValueString() : "";
    }

    @Override
    public float getVariableFloat(String key, float defaultVal) {
        IAnimationVariableSlot iAnimationVariableSlot = this.getVariable(key);
        return iAnimationVariableSlot != null ? iAnimationVariableSlot.getValueFloat() : defaultVal;
    }

    @Override
    public boolean getVariableBoolean(String key) {
        IAnimationVariableSlot iAnimationVariableSlot = this.getVariable(key);
        return iAnimationVariableSlot != null && iAnimationVariableSlot.getValueBool();
    }

    @Override
    public boolean getVariableBoolean(String key, boolean defaultVal) {
        IAnimationVariableSlot iAnimationVariableSlot = this.getVariable(key);
        return iAnimationVariableSlot != null ? iAnimationVariableSlot.getValueBool() : defaultVal;
    }

    @Override
    public Iterable<IAnimationVariableSlot> getGameVariables() {
        return this.m_GameVariables.values();
    }

    @Override
    public boolean isVariable(String name, String val) {
        return StringUtils.equalsIgnoreCase(this.getVariableString(name), val);
    }

    @Override
    public boolean containsVariable(String key) {
        if (StringUtils.isNullOrWhitespace(key)) {
            return false;
        } else {
            String string = key.trim();
            return this.m_GameVariables.containsKey(string);
        }
    }
}
